package com.kk.shiro.jwt;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: kevin
 * @data: 2018-08-25 15:35
 * @desc:
 */
public class JwtToken {
    private static final String SECRET = "kevin";

    public static String createToken() throws UnsupportedEncodingException {
        //签发时间
        Date date = new Date();
        //过期时间：1分钟过期
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, 1);
        Date expiresDate = nowTime.getTime();
        Map<String, Object> map = new HashMap<>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");
        String token = JWT.create()
                .withHeader(map)
                .withClaim("name", "kevin")
                .withClaim("age", "25")
                .withClaim("com", "kk")
                .withExpiresAt(expiresDate)
                .withIssuedAt(date)
                .sign(Algorithm.HMAC256(SECRET));
        return token;
    }
}
